﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework4
{
    internal class Program
    { 
        static void Main(string[] args)
        {
            Random random = new Random();
            double sumArea = 0;
            for(int i=0;i<10;i++)
            {
                int shapeType = random.Next(0, 3);
                //配合后续的switch语句随机生成并选择图形的类型
                string shapeName = "";
                switch(shapeType)
                {
                    case 0:
                        shapeName = "rectangle";
                        break;
                    case 1:
                        shapeName = "square";
                        break;
                    case 2:
                        shapeName = "triangle";
                        break;
                    default:
                        shapeName = "INVALID";
                        break;
                }
                //根据工厂类提供的函数创建并返回
                Shape sh = ShapeFactory.ChooseShape(shapeName);
                Console.Write("第"+(i+1)+ "个添加的图形是："+ shapeName);//Console.Write(i+1);Console.Write("个添加的图形是：");Console.Write(shapeName);
                Console.Write("\t");
                //判断图形是否合法
                //若合法则累加其面积
                //若不合法则不累加
                if (sh.IsValid())
                {
                    double shArea = sh.getArea();
                    sumArea += shArea;
                    Console.Write("该图形的面积是：" + shArea);
                }
                if (!sh.IsValid())
                {
                    Console.Write("生成的图形非法，面积不存在");
                }
                
                Console.WriteLine();
            }
            Console.WriteLine("10个图形的面积和为："+sumArea);
            Console.ReadKey();
        }
        //static void getShape(IShape shape)
        //{
        //    if(shape.IsValid())
        //    {
        //        Console.WriteLine($"面积为：{shape.getArea()}");
        //    }
        //    else
        //    {
        //        Console.WriteLine("形状不合法！");
        //    }
        //}
    }
    //图形接口，包括是否有效判断函数和面积计算函数
    public interface IShape
    {
        double getArea();
        bool IsValid();
    }
   //抽象的图形类
    public abstract class Shape
    {
        public abstract double getArea();
        public abstract bool IsValid();
    }

    //三角形类
    public class Triangle : Shape, IShape
    {
        public double a { get; set; }//三边长
        public double b { get; set; }
        public double c { get; set; }
        public override double getArea()
        {
            double s = (a + b + c) / 2;
            double S = (Math.Sqrt(s * (s - a) * (s - b) * (s - c)));
            return S;
        }
        public override bool IsValid()
        {
            //throw new NotImplementedException();
            bool flag1 = (a + b > c) && (a + c > b) && (b + c > a);
            bool flag2 = (Math.Abs(a - b) < c) && (Math.Abs(a - c) < b) && (Math.Abs(b - c) < a);
            bool flag3 = (a > 0) && (b > 0) && (c > 0);
            return flag1 && flag2 && flag3;
        }
    }
    //长方形类
    public class Rectangle : Shape, IShape
    {
        public double length { get; set; }
        public double width { get; set; }
        public override double getArea()
        {
            return length * width;
        }
        public override bool IsValid()
        {
            return length > 0 && width > 0;
        }
    }
    //正方形类
    public class Square:Shape,IShape
    {
        public double length { get; set; }
        public override double getArea()
        {
            return length * length;
        }
        public override bool IsValid()
        {
            return length > 0;
        }
    }
   //工厂类
    public class ShapeFactory
    {
        //根据传入的参数创建不同的图形实例并返回
        public static Shape ChooseShape(string name)
        {
            Random random = new Random();
            switch(name.ToLower())
            {
                case "triangle":
                    return new Triangle { a = random.Next(1, 10), b = random.Next(1, 10), c = random.Next(1, 10) };
                    //break;
                case "rectangle":
                    return new Rectangle { length = random.Next(1, 10), width = random.Next(1, 10) };
                   // break;
                case "square":
                    return new Square { length = random.Next(1, 10) };
                   // break;
                default:
                    Console.WriteLine("invalid!");
                    throw new ArgumentException("Unsupported shape type.");
                    break;
            }
        }
    }
}
